package com.example.jdbc2.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyDataSourceConfiguration {

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 因为druid没有自动配置类，只能通过手动来配置
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource myDataSource() {
        return new DruidDataSource();
    }

    /**
     * 配置Druid的监控
     * 1.配置一个管理后台的servlet
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {

        StatViewServlet statViewServlet = new StatViewServlet();


        ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>();

        bean.setServlet(statViewServlet);

        /**
         * Servlet Url映射只有一个* 即 /druid/*
         * 如果只映射/druid/，则/druid/login.html就不能跳转到servlet了
         * /druid/** 则404
         */
        bean.setUrlMappings(Arrays.asList("/druid/*"));

        /**
         * 因为 com.alibaba.druid.support.http.StatViewServlet 的父类 com.alibaba.druid.support.http.ResourceServlet
         * 的属性 loginUsername、loginPassword 等没有set()方法，所以只能使用初始参数的方式对其进行赋值
         */
        Map<String, String> initParametersMap = new HashMap<>();

        initParametersMap.put("loginUsername", "admin");
        initParametersMap.put("loginPassword", "a1!");
        initParametersMap.put("allow","");//默认允许所有ip访问
        initParametersMap.put("deny", "192.168.33.33");//拒绝此ip登陆


        bean.setInitParameters(initParametersMap);

        logger.info("DuridServlet初始化完成========");

        return bean;
    }

    /**
     * 2.配置一个web监控的filter
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();

        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();

        bean.setFilter(webStatFilter);

        bean.setUrlPatterns(Arrays.asList("/druid/**"));

        Map<String, String> initParametersMap = new HashMap<>();

        /**
         * 同样在这里设置的是WebStatFilter的属性
         */
        initParametersMap.put("exclusions","*.js,*.css,/druid/**");

        bean.setInitParameters(initParametersMap);

        logger.info("DruidFilter初始化完成===========");

        return bean;
    }

}
